package com.example.demo.core.utils;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.List;

/**
 * 计算字节数
 */
public class ByteSizeUtil {
    public  static  int  getByteSize(List<?> datas) {
        int byteSize=0;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream os = new ObjectOutputStream(baos);
            os.writeObject(datas);
            os.close();
            byteSize=baos.size();

            baos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byteSize;
    }

    public static   int  getByteSize(Object  data){
        int byteSize=0;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream os = new ObjectOutputStream(baos);
            os.writeObject(data);
            os.close();
            byteSize=baos.size();

            baos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byteSize;
    }

    public static void main(String[] args) throws  Exception{
        int n = 2000;
        List<Integer> list = new LinkedList<Integer>();
        for (int i = 0; i < n; i++) {
            list.add(i);
        }
        getByteSize(list);
    }
}
